use axum::{Extension, Json};
use crate::core::engine::{OpsPanelCore, ResourceStats};
use serde::Serialize;
use std::time::SystemTime;

#[derive(Serialize)]
pub struct HealthStatus {
    status: String,
    version: String,
    uptime_seconds: u64,
    active_tasks: usize,
    memory_usage_mb: f64,
    cpu_usage_percent: f64,
    supported_languages: Vec<String>,
    git_commit: String,
    build_time: String,
}

pub async fn health_check_handler(
    Extension(core): Extension<OpsPanelCore>,
) -> Json<HealthStatus> {
    let stats = core.get_resource_stats().await;
    
    Json(HealthStatus {
        status: "healthy".to_string(),
        version: env!("CARGO_PKG_VERSION").to_string(),
        uptime_seconds: core.uptime(),
        active_tasks: stats.active_tasks,
        memory_usage_mb: stats.memory_usage,
        cpu_usage_percent: stats.cpu_usage,
        supported_languages: core.config.supported_languages.clone(),
        git_commit: env!("GIT_HASH").to_string(),
        build_time: env!("BUILD_TIMESTAMP").to_string(),
    })
}

pub async fn get_stats_handler(
    Extension(core): Extension<OpsPanelCore>,
) -> Json<ResourceStats> {
    Json(core.get_resource_stats().await)
}